const request=require('../../util/request.js')
// import util from '../../utils/util.js';
import util from '../../util/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_time:"请选择采摘日期",//采摘日期
    mindate:Math.round((new Date() - 365*86400*1000)),
    p_name:"",//项目名称
    delivery_time:"请选择运输日期",//交货日期
    order_no:"",//订单号
    delivery_address:"",//交货地址
    id:"",//id
    show:false,
    date:"",
    showstarttime:false,//采摘日期，例2025
    showlasttime:false,//运输日期
    showjiagongtime:false,//加工日期
    //---------------新的
    // showjiagongtime:false,
    id: 0,//传0是第一次进入时，传1是编辑 <number>
    fid: "1180",//提交时候传回来就行，不用管 <string>
    // picking_date: "",//采摘日期，例2025-11-13 <string>
    picking_people: "",//不用管 <string>
    // transport_date: "",//运输日期 <string>
    transport_car_no: "",//运输车辆牌号 <string>
    transport_lock_no: "",//运输锁号 <string>
    picking_total: "",//运输框数 <string>
    machining_date: "",//加工日期 <string>
    picking_pics: [ ],//采摘图片数组
    transport_pics: [ ],//运输图片数组
    machining_pics: [ ]//加工图片数组
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options,"===fid")
    var that = this 
    var params = {
      fid:options.fid,
    }
    this.setData({
      fid:options.fid
    })
    // return
    // that.setData({p_no:options})
    request.requestGet('fruit/get_plan_info', params, '',function (res) {
      console.log(res,"ressssssss")
      that.setData({
        order_time:res.data.picking_date,
        delivery_time:res.data.transport_date,
        transport_car_no:res.data.transport_car_no,
        picking_total:res.data.picking_total,
        machining_date:res.data.machining_date,
        transport_lock_no:res.data.transport_lock_no,
        transport_pics:res.data.transport_pics,
        machining_pics:res.data.machining_pics,
        picking_people:res.data.picking_people,
        picking_pics:res.data.picking_pics,
        transport_pics:res.data.transport_pics,
        machining_pics:res.data.machining_pics,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  submit(event){
    var that = this
    let params = {
      id:that.data.id,
      transport_car_no:that.data.transport_car_no,//运输车牌号
      transport_lock_no:that.data.transport_lock_no,//运输锁号
      picking_total:that.data.picking_total,//运输框数
      order_time:that.data.order_time,//采摘日期
      delivery_time:that.data.delivery_time,//运输日期
      machining_date:that.data.machining_date,//加工日期
      fid:that.data.fid,
      picking_people:that.data.picking_people,
    }
    request.requestAjax('fruit/save_plan_info',params,"提交中",function(res){
      if(res.status==200){
        wx.showToast({
          title: '操作成功',
          icon: 'success',
          duration: 1000,
          success: function() {
            setTimeout(function() {
              wx.navigateBack() 
            }, 1000);
          }
        });
        return false;
      }else{
        wx.showToast({
          title: res.msg,
          icon:"none"
        })
      }

  },function(res){
    wx.showToast({
      title: '请求失败',
      icon:"none"
    })
  });
  },
  bindKeyInputVant(event){
    console.log(event);
    let valueName = event.target.dataset.variable;
    this.setData({
      [valueName]:event.detail
    });
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ showstarttime: false });
    this.setData({ showlasttime: false });
    this.setData({ showjiagongtime: false });
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1]),
      findtime : this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1])
    });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
  },
  showstart() {
    this.setData({ showstarttime: true });
  },
  
  showlast() {
    this.setData({ showlasttime: true });
  },

  start(event) {
    const tmptime = this.formatDate(event.detail)
    this.setData({
      order_time:tmptime
    })
    this.onClose()
  },

  last(event) {
    const tmptime = this.formatDate(event.detail)
    this.setData({
      delivery_time:tmptime
    })
    this.onClose()
  },
  showjiagong(){
      this.setData({ showjiagongtime: true });
  },
  jia_gong_day(event){
    const tmptime = this.formatDate(event.detail)
    this.setData({
      machining_date:tmptime
    })
    this.onClose()
  },

  // delPic(e){
  //   var that = this;
  //   console.log(e,"eeeeeeeeeeeee")
  //   var picarrname = e.currentTarget.dataset.picarrname;
  //   let fileList = that.data[picarrname];

  //   fileList.splice(e.detail.index,1);
  //   that.setData({
  //     [picarrname]:fileList
  //   })
  // },
  delPic(e){
    console.log(e,"eeeeeeeeeee")
    var that = this;
    var index = e.currentTarget.dataset.index
    const picking_pics = that.data.picking_pics;
    picking_pics.splice(index,1)
    that.setData({ picking_pics });
  },

  uploadImage(){
    var that = this;
    util.uploadImageOne('upload/image', function (res){
      if(res.status==200){
          const picking_pics = that.data.picking_pics;
          picking_pics.push({pic:res.data.url,desc:""});
          that.setData({ picking_pics });
          console.log(that.data.picking_pics)
      }else{
        wx.showToast({
          title: '上传失败',
        })
      }
    });
  },

  set_transport_img(){
    var that = this;
    util.uploadImageOne('upload/image', function (res){
      if(res.status==200){
          const transport_pics = that.data.transport_pics;
          transport_pics.push({pic:res.data.url,desc:""});
          that.setData({ transport_pics });
          console.log(that.data.transport_pics)
      }else{
        wx.showToast({
          title: '上传失败',
        })
      }
    });
  },
  
  delPic_transport_img(e){
    console.log(e,"eeeeeeeeeee")
    var that = this;
    var index = e.currentTarget.dataset.index
    const transport_pics = that.data.transport_pics;
    transport_pics.splice(index,1)
    that.setData({ transport_pics });
  },
  set_machining_pics(){
    var that = this;
    util.uploadImageOne('upload/image', function (res){
      if(res.status==200){
          const machining_pics = that.data.machining_pics;
          machining_pics.push({pic:res.data.url,desc:""});
          that.setData({ machining_pics });
          console.log(that.data.machining_pics)
      }else{
        wx.showToast({
          title: '上传失败',
        })
      }
    });
  },
  delPic_machining_pics(e){
    console.log(e,"eeeeeeeeeee")
    var that = this;
    var index = e.currentTarget.dataset.index
    const machining_pics = that.data.machining_pics;
    machining_pics.splice(index,1)
    that.setData({ machining_pics });
  },


})